import {api} from './libs/api.js';
let _self;
export const index =  {
	data() {
		return {
			optLock:false,
			tabItem:0,
			tabSubItem:0,
			icons:[
				{type:"search",size:uni.upx2px(30)},
				{type:"scan",size:uni.upx2px(30)},
				{type:"search",size:uni.upx2px(40)},
			],
			pcategorys:[],
			proTypeIndex:1,
			proType:api.proType,
			categorys:[],
			dataList:[],
			selectInfo:null
		}
	},
	onReachBottom:function(e){
		this.params.page+=1;
		this.initData();
	},
	
	mounted() {
		_self = this;
	},
	
	methods: {
		
		async selectTab(event){
			//console.log(event);
			let level=event.currentTarget.dataset.level;

			if(level=="1"){
				this.tabItem=event.currentTarget.dataset.id;	
				this.tabSubItem=0;
				this.categorys= await api.categoryList({"pid":this.tabItem});
			}else if(level=="2"){
				this.tabSubItem=event.currentTarget.dataset.id;
			}else if(level=="3"){
				this.proTypeIndex=event.currentTarget.dataset.id;
				this.tabSubItem=0;
				this.tabItem=0;
			}
			
			this.initData();
		},
		async initData(){
			uni.showLoading({title: '加载中'});
			this.params["pcategory_id"]=this.tabItem;
			this.params["category_id"]=this.tabSubItem;
	
			this.params["pro_type"]=this.proTypeIndex;
			
			let dataInfo=await api.productList(this.params);
			if(this.params.page==1){
				this.dataList=dataInfo["dataList"];
			}else{
				if(dataInfo["dataList"].length==0) this.params.page-=1;
				this.dataList.push.apply(this.dataList,dataInfo["dataList"]);
			}
			
			this.pcategorys= await api.categoryList({"pid":0,"c_type":this.proTypeIndex});
			uni.hideLoading();
		},
		
		searchCancel(){
			this.params.name="";
			this.initData();
		},
		
		
		
		/**
		* 扫码事件
		*/
		scanCodeEvent: function(){
			var that = this;
			uni.scanCode({
				success: async function (res) {

					let scanInfo=await api.scanCodeInfo({"code":res.result});
					if(scanInfo["product"]){
						uni.navigateTo({url:"/pages/scan_code/index?code="+res.result});
					}else if(scanInfo["deliver"]){
						uni.navigateTo({url:"/pages/deliver_confirm/index?status=1&code="+res.result});
					}else if(scanInfo["order"]){
						uni.navigateTo({url:"/pages/refund/add/index?code="+res.result});
					}else{
						uni.showToast({icon:"none",title:"二维码无效！"});
					}

				}
			});
		},
		
		
	
		seachData(){this.initData();}
				
	},
}